using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Text;
using System.Xml.Serialization;
using Mud.Common;

namespace Mud.Server
{
	public sealed class ServerConfiguration
	{


		public const string configurationFile = "Configuration.xml";

        #region ServerConfiguration properties

		public int NetworkClientPort {
			get { return _networkClientPort; }
			set { _networkClientPort = value; }
		}
		private int _networkClientPort = 6001;

		public int TickDuration {
			get { return _tickDuration; }
			set { _tickDuration = value; }
		}
		private int _tickDuration = 100;

		public string ServerName {
			get { return _serverName; }
			set { _serverName = value; }
		}
		private string _serverName;


		#endregion

		public static ServerConfiguration LoadConfiguration() {
			XmlSerializer serializer = new XmlSerializer(typeof(ServerConfiguration));
			try {
				using (StreamReader reader = new StreamReader(configurationFile)) {
					ServerConfiguration configuration = (ServerConfiguration)serializer.Deserialize(reader);
					return configuration;
				}
			} catch (Exception ex) {
                Logger.Log(LogType.Warning, "Configuration", "Unable to load the configuration file, using default values. Error: {0}", ex.Message);
				return new ServerConfiguration();
			}
		}

		public void SaveConfiguration() {
			try {
				XmlSerializer serializer = new XmlSerializer(typeof(ServerConfiguration));
				using (StreamWriter writer = new StreamWriter(configurationFile)) {
					serializer.Serialize(writer, this);
				}
			} catch (Exception ex) {
                Logger.Log(LogType.Warning, "Configuration", "Unable to save the configuration file. Error: {0}", ex.Message);
			}
		}

	}
}
